﻿using FarseerPhysics.Dynamics.Contacts;
using IcicleFramework.Actions.Damage;
using IcicleFramework.Components.Behaviors;
using IcicleFramework.Components.Behaviors.CollisionBehaviors;
using IcicleFramework.Components.Damage;
using IcicleFramework.Components.Physics;

namespace ExampleGame.Components.Behaviors
{
    public class BallCollisionBehavior : PhysicsCollisionBehavior
    {
        protected IDamageComponent damageComponent;

        public BallCollisionBehavior() { }

        public BallCollisionBehavior(BallCollisionBehavior old) : base(old) { }

        public override IBehavior DeepClone()
        {
            return new BallCollisionBehavior(this);
        }

        public override void Initialize()
        {
            damageComponent = ParentGameObject.GetComponent<IDamageComponent>();
            base.Initialize();
        }

        protected override void OnCollision(IPhysicsComponent physicsComponent, IPhysicsComponent colliding, Contact contact)
        {
            //Fire the damage action.
            var damageAction = Parent.ActionFactory.GenerateAction<BasicDamageAction>();
            damageAction.Damage = damageComponent.Damage;
            damageAction.Target = colliding.Parent;

            Parent.FireAction(damageAction);
        }
    }
}
